#ifndef __APP_CAN_H
#define __APP_CAN_H

#include "gd32f4xx.h"
#include <stdint.h>
#include "driver/can/drv_can0.h"
#include "driver/can/drv_can1.h"
#include "hems_can_def.h"

#define CMD_RX_CH can_rx0
#define DATA_RX_CH can_rx1
#define ACK_TX_ID 0x700
typedef enum {
  can_rx0 = 0, // 0x7A0
  can_rx1 = 1,
  can_rx_num
} can_flt;

typedef struct user_can_rx_msg_tag {
  uint32_t id;        /*!< identifier */
  uint32_t write_cnt; /*!< 接收总次数统计 */
  uint32_t read_cnt;  /*!< 读取总次数统计 */
  uint8_t  n;           /*!<
                      表明rx_data_buf的大小,如rx_dlen为8个字节，表示rx_data_buf的大小为n*8字节*/
  uint8_t rx_ff;   /*!< format of frame, standard or extended format */
  uint8_t rx_ft;   /*!< type of frame, data or remote */
  uint8_t rx_dlen; /*!< data length */
  volatile uint8_t (*rx_data_buf)[8]; /*!< receive data */
} user_can_rx_info_t;

typedef struct user_can_rx_data_t {
  uint32_t write_cnt; /*!< 接收总次数统计 */
  uint32_t read_cnt;  /*!< 读取总次数统计 */
  uint8_t update;     /*!< 是否更新标志 */
  uint8_t rx_dlen;    /*!< data length */
  uint8_t data[8];    /*!< receive data */
} user_can_rx_data_t;

extern volatile user_can_rx_info_t *vg_pCanRxMsg[can_rx_num];
void can_std_filter_list_mode_cfg(uint16_t filter_numb,
                                  uint16_t filter_fifo_numb, uint16_t id);
int can_transmit_message(uint32_t id, uint8_t ide, uint8_t *data,
                         uint8_t length);
void read_can_rx_msg(uint8_t rx_fi, user_can_rx_data_t *rx_data);
void can_user_init(void);
uint32_t CAN_recv_raw(user_can_rx_info_t * can_msg);
int32_t sys_can_init(uint32_t baud, uint32_t node_id);
void canbus_init(void);
#endif
